/*
-----------------------------------------------------------------------------
This source file is part of OGRE-Next
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2017 Torus Knot Software Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef _OgreDescriptorSetSampler_H_
#define _OgreDescriptorSetSampler_H_

#include "OgrePrerequisites.h"

#include "OgreCommon.h"

#include "OgreHeaderPrefix.h"

namespace Ogre
{
    /** \addtogroup Core
     *  @{
     */
    /** \addtogroup Resources
     *  @{
     */

    /** Descriptor sets describe what samplers should be bound together in one place.
        Must be created via HlmsManager.
        See DescriptorSetTexture.
    @remarks
        Do not create and destroy a set every frame. You should create it once, and reuse
        it until it is no longer necessary, then you should destroy it.
    */
    struct _OgreExport DescriptorSetSampler
    {
        uint16 mRefCount;
        uint16 mShaderTypeSamplerCount[NumShaderTypes];
        void  *mRsData;  ///< Render-System specific data

        FastArray<const HlmsSamplerblock *> mSamplers;

        DescriptorSetSampler() : mRefCount( 0 ), mRsData( 0 )
        {
            memset( mShaderTypeSamplerCount, 0, sizeof( mShaderTypeSamplerCount ) );
        }

        bool operator!=( const DescriptorSetSampler &other ) const
        {
            const size_t thisNumSamplers = mSamplers.size();
            if( thisNumSamplers != other.mSamplers.size() )
                return true;

            for( size_t i = 0; i < thisNumSamplers; ++i )
            {
                if( this->mSamplers[i] != other.mSamplers[i] )
                    return true;
            }

            for( size_t i = 0; i < NumShaderTypes; ++i )
            {
                if( this->mShaderTypeSamplerCount[i] != other.mShaderTypeSamplerCount[i] )
                    return true;
            }

            return false;
        }

        bool operator<( const DescriptorSetSampler &other ) const
        {
            const size_t thisNumSamplers = mSamplers.size();
            if( thisNumSamplers != other.mSamplers.size() )
                return thisNumSamplers < other.mSamplers.size();

            for( size_t i = 0; i < thisNumSamplers; ++i )
            {
                if( this->mSamplers[i] != other.mSamplers[i] )
                    return this->mSamplers[i] < other.mSamplers[i];
            }

            for( size_t i = 0; i < NumShaderTypes; ++i )
            {
                if( this->mShaderTypeSamplerCount[i] != other.mShaderTypeSamplerCount[i] )
                    return this->mShaderTypeSamplerCount[i] < other.mShaderTypeSamplerCount[i];
            }

            return false;
        }

        void checkValidity() const
        {
#if OGRE_DEBUG_MODE >= OGRE_DEBUG_MEDIUM
            size_t totalSamplersUsed = 0u;

            for( size_t i = 0; i < NumShaderTypes; ++i )
                totalSamplersUsed += mShaderTypeSamplerCount[i];

            OGRE_ASSERT_MEDIUM(
                totalSamplersUsed > 0 &&
                "This DescriptorSetSampler doesn't use any sampler! Perhaps incorrectly setup?" );
            OGRE_ASSERT_MEDIUM( totalSamplersUsed == mSamplers.size() &&
                                "This DescriptorSetSampler doesn't use as many samplers as it "
                                "claims to have, or uses more than it has provided" );
#endif
        }
    };

    /** @} */
    /** @} */
}  // namespace Ogre

#include "OgreHeaderSuffix.h"

#endif
